package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.reflect.TypeToken;

/**
 * 个人中心->我的设备
 * 
 * @author G
 * 
 */
public class ShebeiActivity extends BaseActivity {
	private TextView tv_title_title;
	private EditText idshebei;
	private TextView bangding;
	private final static int SCANNIN_GREQUEST_CODE = 1;
	private Map<String, String> planList = new HashMap<String, String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_geren_shebei);
		initView();

	}

	private void initView() {
		initTitleBar();
		idshebei = (EditText) findViewById(R.id.idshebei);

		bangding = (TextView) findViewById(R.id.bangding);
		bangding.setOnClickListener(this);
		getint();
	}

	private void initTitleBar() {
		TextView tv_title_back =(TextView) findViewById(R.id.tv_title_back);
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		tv_title_title.setText(getString(R.string.bangding));
		TextView tv_title_othuu =(TextView) findViewById(R.id.tv_title_othuu);
		tv_title_othuu.setVisibility(View.VISIBLE);
		tv_title_othuu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent mianIntent = new Intent(ShebeiActivity.this,
						MipcaActivityCapture.class);
				mianIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivityForResult(mianIntent, SCANNIN_GREQUEST_CODE);
				
			}
		});
	}

	private Handler bbHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String dataStr = data.getString(HttpCons.DATA);
			if (state.equals("001")) {

				planList = HttpServer.gson.fromJson(dataStr,
						new TypeToken<Map<String, String>>() {
						}.getType());
				if (planList.get("devicesn") != null) {
					idshebei.setText(planList.get("devicesn"));
				}

				Toast.makeText(ShebeiActivity.this, "操作成功", 0).show();
			} else {
				Toast.makeText(ShebeiActivity.this, "msg", 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(ShebeiActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void getint() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("aliasAccount", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setChannelId("otrue_android_patient_channel");
		parameter.setData(map);
		new HttpServer().post(HttpCons.queryDevicesn, parameter, bbHandler);

	};

	private Handler Handler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				Toast.makeText(getApplicationContext(), msg, 0).show();
				finish();

			} else {
				Toast.makeText(getApplicationContext(), msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(ShebeiActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
		case SCANNIN_GREQUEST_CODE:
			if(resultCode == RESULT_OK){
				Bundle bundle = data.getExtras();
				//��ʾɨ�赽������
			Log.e("AAAA", bundle.getString("result"))	;
				idshebei.setText(bundle.getString("result"));
				//��ʾ
				
			}
			break;
		}
    }	
	
	
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.bangding:
			if (idshebei.getText().toString() == null) {
				Toast.makeText(getApplicationContext(), "设备ID不能为空", 0).show();
			} else if (idshebei.getText().toString().length() > 50) {
				Toast.makeText(getApplicationContext(), "设备ID输入错误", 0).show();
			} else {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("devicesn", idshebei.getText().toString());
				TransferParameter parameter = new TransferParameter();
				parameter.setUsername(MyConst.getACCOUNT());
				parameter.setPassword("");
				parameter.setData(map);
				new HttpServer().post(HttpCons.PatientBaseInfo, parameter,
						Handler);
				break;
			}

		default:
			break;
		}

	}

}
